import uiutil
import blue
import log
import uicls
import uix
import xtriui
VALID_ICON_ROOTS = ['corps',
 'alliance',
 'icons']

def ParseIconPath(path):
    try:
        (root, textureNo, iconSize, iconNo,) = path.split('_')
        iconSize = int(iconSize)
        iconNo = int(iconNo)
    except:
        log.LogError('ParseIconPath; Invalid path, should be "root_textureNo_iconSize_iconNo" ', path)
        return ('res:/ui/texture/icons/icons05.dds', '05', 64, 10)
    if (root == 'ui'):
        root = 'icons'
    if (root in VALID_ICON_ROOTS):
        texturePath = ('res:/ui/texture/%s/%s%s.dds' % (root,
         root,
         textureNo.zfill(2)))
    else:
        texturePath = ('res:/%s/texture/icons/%s_%s.dds' % (root,
         textureNo.zfill(3),
         iconSize))
    if (texturePath not in uiutil.textureExistanceCache):
        f = blue.ResFile()
        if not f.FileExists(texturePath):
            import uiconst
            return uiconst.FALLBACKICON
        uiutil.textureExistanceCache.append(texturePath)
    return (texturePath,
     textureNo,
     iconSize,
     iconNo)


uiutil.ParseIconPath = ParseIconPath

def PrepareDrag_Standard(dragContainer, dragSource, *args):
    dad = dragContainer
    x = 0
    y = 0
    iconSize = 64
    for node in dragContainer.dragData:
        (nameSpace, className,) = node.__guid__.split('.')
        ns = Import(nameSpace)
        decoClass = getattr(ns, className, None)
        icon = uicls.DraggableIcon()
        icon.left = (x * (iconSize + 10))
        icon.top = (y * (iconSize + 10))
        if (((decoClass is not None) and issubclass(decoClass, xtriui.InvItem)) or (node.__guid__ in ('xtriui.FittingSlot', 'xtriui.ModuleButton', 'xtriui.ShipUIModule', 'xtriui.CertSlot'))):
            typeID = node.rec.typeID
            MakeTypeIcon(icon, dad, typeID, iconSize, isCopy=getattr(node, 'isCopy', False))
        if (node.__guid__ in ('xtriui.TypeIcon', 'listentry.DraggableItem')):
            icon.ChangeIcon(typeID=node.typeID, size=iconSize)
        elif (node.__guid__ in ('listentry.User', 'listentry.Sender', 'listentry.ChatUser', 'listentry.SearchedUser')):
            charinfo = (node.info or cfg.eveowners.Get(node.charID))
            uix.GetOwnerLogo(icon, node.charID, iconSize, noServerCall=False)
        elif (node.__guid__ in ('listentry.PlaceEntry',)):
            icon.ChangeIcon(icon='ui_09_64_01')
        elif (node.__guid__ in ('listentry.NoteItem',)):
            icon.ChangeIcon(icon='ui_49_64_03')
        elif (node.__guid__ in ('listentry.QuickbarItem', 'listentry.GenericMarketItem')):
            typeID = node.typeID
            MakeTypeIcon(icon, dad, typeID, iconSize)
        elif (node.__guid__ in ('listentry.VirtualAgentMissionEntry',)):
            icon.ChangeIcon(icon='ui_25_64_03')
        elif node.__guid__.startswith('listentry.ContractEntry'):
            iconName = 'ui_64_64_10'
            if ('Auction' in node.__guid__):
                iconName = 'ui_64_64_16'
            elif ('ItemExchange' in node.__guid__):
                iconName = 'ui_64_64_09'
            elif ('Courier' in node.__guid__):
                iconName = 'ui_64_64_13'
            icon.ChangeIcon(icon=iconName)
        elif (node.__guid__ in ('listentry.FleetFinderEntry',)):
            icon.ChangeIcon(icon='ui_53_64_16')
        elif (node.__guid__ == 'xtriui.ListSurroundingsBtn'):
            icon.ChangeIcon(typeID=node.typeID, itemID=node.itemID, size=iconSize)
        elif (node.__guid__ == 'listentry.PaletteEntry'):
            icon.ChangeIcon(typeID=node.id, itemID=node.id, size=iconSize)
        elif (node.__guid__ == 'listentry.DungeonTemplateEntry'):
            uix.MapSprite('74_15', uiutil.GetChild(icon, 'icon'))
        elif (node.__guid__ in ('listentry.SkillEntry', 'listentry.SkillQueueSkillEntry')):
            icon.ChangeIcon(typeID=node.invtype.typeID, size=iconSize)
        elif (node.__guid__ in ('listentry.FittingEntry',)):
            icon.ChangeIcon(icon='ui_17_128_04')
        elif (node.__guid__ in ('listentry.MailEntry',)):
            icon.ChangeIcon(icon='ui_94_64_01')
        elif (node.__guid__ in ('listentry.CorpAllianceEntry',)):
            icon.ChangeIcon(icon='ui_07_64_10')
        elif (node.__guid__ in ('neocom.BtnDataNode',)):
            icon.ChangeIcon(icon=node.iconPath)
        x += 1
        if (x >= 3):
            x = 0
            y += 1
        dad.children.append(icon)
        icon.state = uix.UI_DISABLED
        if (y > 2):
            break

    sm.GetService('audio').StartSoundLoop('DragDropLoop')
    dragContainer.dragSound = 'DragDropLoop'
    return (0, 0)



def MakeTypeIcon(icon, dad, typeID, iconSize, isCopy = False):
    techIcon = uix.GetTechLevelIcon(None, typeID=typeID)
    if techIcon:
        techIcon.left = icon.left
        techIcon.top = icon.top
        dad.children.append(techIcon)
    icon.ChangeIcon(typeID=typeID, size=iconSize, isCopy=isCopy)


uiutil.PrepareDrag_Standard = PrepareDrag_Standard

